package com.yizn.learn.concurrent;

import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author yisl
 * @create 2019-08-29 16:41
 */
public class test {

    static {
        System.out.println("test 加载");
    }
    public static void test1() throws InterruptedException {
        long start = System.currentTimeMillis();
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        final AtomicInteger atomicInteger = new AtomicInteger();
        int size = 1000;
        final CountDownLatch countDownLatch = new CountDownLatch(size);
        for (int i=0;i<size;i++){
            final int finalI = i;
            executorService.submit(()->{
                atomicInteger.updateAndGet(pre ->{
                    int v =pre + finalI;

                    return v;

                });
                countDownLatch.countDown();
            });
        }
        countDownLatch.await();
        System.out.println(atomicInteger.get());
        System.out.println("耗时"+(System.currentTimeMillis()-start));
        executorService.shutdown();

    }

    public static void main(String[] args) throws InterruptedException {
        test1();
    }
}
